package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public Result order(Map map) throws Exception {
        //先把预约类型加进map
        map.put("orderType", Order.ORDERTYPE_WEIXIN);
        //将日期取出来,进行查询日期的存在
        String orderDate = (String) map.get("orderDate");
        //数据库查询时需要用到日期类型,用到dataUtil
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(date);
        //对查询结果进行判断,如果为空,说明当天没有进行预约,不为空,比较人数
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (number == reservations) {//说明预约已满,不能进行预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //可以进行预约,预约时,将预约设置表的revervations+1,将预约的信息,加入到order表,判断用户是否为会员,不是会员自动完成会员表的添加
        //上面逻辑有问题
        //判断用户是否是会员(根据手机号)
        String telephone = (String) map.get("telephone");
        Member member = memberMapper.findByTel(telephone);//根据预约的表进行查询
        if (member != null) {//是会员,查看是否进行预约,查询order表
            int setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(member.getId(), date, setmealId);
            List<Order> list = orderMapper.findOrderByMemberId(order);
            if (list != null && list.size() > 0) {//说明该用户已经进行了预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }
        //可以进行预约,reversation+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingMapper.editReservationsByOrderDate(orderSetting);
        //该用户不是会员,进行注册
        if (member == null) {
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            memberMapper.regist(member);
        }
        //添加order表的预约信息
        Order order = new Order(member.getId(), date, (String) map.get("orderType"), Order.ORDERSTATUS_NO, Integer.parseInt((String) map.get("setmealId")));
        orderMapper.add(order);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderMapper.findDetialById(id);
        return map;

    }
}
